package cn.xhjava.crm.settings.controller;

import cn.xhjava.crm.commons.ReturnObject;
import cn.xhjava.crm.commons.SysContants;
import cn.xhjava.crm.commons.util.ExceptionUtil;
import cn.xhjava.crm.commons.util.UUIDUtils;
import cn.xhjava.crm.settings.domain.DicValue;
import cn.xhjava.crm.settings.service.DicTypeService;
import cn.xhjava.crm.settings.service.DicValueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   XiaHu
 * Date:     2023/6/20
 * 系统设置
 */
@Controller
public class DicValueContoller {
    private static final Logger log = LoggerFactory.getLogger(DicValueContoller.class);

    @Autowired
    private DicValueService dicValueService;

    @Autowired
    private DicTypeService dicTypeService;

    @RequestMapping("/settings/dictionary/value/index")
    public String dictionaryValueIndex(HttpServletRequest request) {
        List<DicValue> dicValueList = null;
        try {
            dicValueList = dicTypeService.selectAll();
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
        }
        request.setAttribute("dicValueList", dicValueList);
        return "settings/dictionary/value/index";
    }

    @RequestMapping("/settings/dictionary/value/queryForPage")
    public @ResponseBody
    Object selectForPage(int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginNo", (pageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        List<DicValue> dicValueList = null;
        int totalRows = 0;
        try {
            dicValueList = dicValueService.selectForPage(map);
            totalRows = dicValueService.selectCountForPage(map);
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
        }

        Map<String, Object> retMap = new HashMap<>();
        retMap.put("dicValueList", dicValueList);
        retMap.put("totalRows", totalRows);
        return retMap;
    }

    // 删除
    @RequestMapping("/settings/dictionary/value/delete")
    public @ResponseBody
    Object delete(String[] id) {
        ReturnObject returnObject = new ReturnObject();
        try {
            int ret = dicValueService.delete(id);
            if (ret > 0) {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_SUCCESS);
            } else {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试....");
            }
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
            returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试....");
        }
        return returnObject;
    }

    @RequestMapping("/settings/dictionary/value/add")
    public @ResponseBody
    Object add(DicValue dicValue) {
        ReturnObject returnObject = new ReturnObject();
        dicValue.setId(UUIDUtils.getUUID());
        try {
            int ret = dicValueService.add(dicValue);

            if (ret > 0) {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_SUCCESS);
            } else {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙,请稍后重试....");
            }
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
            returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙,请稍后重试....");
        }

        return returnObject;
    }


    @RequestMapping("/settings/dictionary/value/queryById")
    public @ResponseBody
    Object selectById(String id) {
        //调用service层方法，查询市场活动
        DicValue dicValue = null;
        try {
            dicValue = dicValueService.selectDetailById(id);
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
        }
        return dicValue;
    }


    @RequestMapping("/settings/dictionary/value/update")
    public @ResponseBody
    Object update(DicValue dicValue) {
        ReturnObject returnObject = new ReturnObject();
        try {
            int ret = dicValueService.update(dicValue);
            if (ret > 0) {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_SUCCESS);
            } else {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙,请稍后重试....");
            }
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
            returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙,请稍后重试....");
        }
        return returnObject;
    }
}
